<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="<s:url value="/style.css"/>" rel="stylesheet" type="text/css"/>
<title>Edit Visitation</title>
<script type="text/javascript">
function addRowToTable()
{
		var tbl = document.getElementById('drugTable');
		var lastRow = tbl.rows.length - 2;
		// if there's no header row in the table, then iteration = lastRow + 1
		var iteration = lastRow;
		var row = tbl.insertRow(lastRow);
		// left cell
		var cellLeft = row.insertCell(0);
		/* var textNode = document.createTextNode(iteration); */

		// select cell
		/* var cellRightSel = row.insertCell(2); */
		var sel = document.createElement('select');
		sel.name = 'drugId';
		<s:iterator value="retrievedDrugs">
		sel.options.add(new Option("<s:property value="drugName"/>",
				"<s:property value="drugId"/>"));
		</s:iterator>
		/* cellRightSel.appendChild(sel); */
		cellLeft.appendChild(sel);
		// right cell
		var cellRight = row.insertCell(1);
		var el = document.createElement('input');
		el.type = 'text';
		el.name = 'dosage';
		cellRight.appendChild(el);
		
		var cellDelete = row.insertCell(2);
		var link = document.createElement("a");
		link.setAttribute("href", "#");
		link.title = "delete";
		link.innerHTML = link.title;
		link.onclick = function(){removeDrugFromTable(row);};
		
		cellDelete.appendChild(link);
	}
function removeDrugFromTable(row){
	var tbl = document.getElementById('drugTable');
	tbl.deleteRow(row.rowIndex);
}
</script>
</head>
<body>
	<jsp:include page="/navigation/navigation.jsp"/>
		<div id="mainBox">

			<center><h2>Update Visitation</h2></center>
			<center>

					<table width="400px">
						<tr>
							<td>ID:</td>
							<td><s:property value="#session.retrievedVisitationContent.visitation.visitationId"/></td>
						</tr>
						<tr>
							<td>Date:</td>
							<td><s:property value="#session.retrievedVisitationContent.visitation.appointment.appointmentDate"/></td>
						</tr>
						<tr>
							<td>Start time:</td>
							<td><s:property value="#session.retrievedVisitationContent.visitation.appointment.startTime"/></td>
						</tr>
						<tr>
							<td>End time:</td>
							<td><s:property value="#session.retrievedVisitationContent.visitation.appointment.endTime"/></td>
						</tr>
						<tr>
							<td>Doctor Name:</td>
							<td><s:property value="#session.retrievedVisitationContent.visitation.appointment.doctor.name"/></td>
						</tr>
						<tr>
							<td>Location:</td>
							<td><s:property value="#session.retrievedVisitationContent.getVisitation().getAppointment().getLocation()"/></td>
						</tr>
						</table>
		<s:form name="editVisitationForm" action="updateVisitation" method="post">
	   					<s:textarea name="retrievedVisitationContent.diagnosis"
					label="Diagnosis" cols="27" rows="5" />
				<s:textarea name="retrievedVisitationContent.treatment"
					label="Treatment" cols="27" rows="5" />
				<s:textarea name="retrievedVisitationContent.comment"
					label="Comment" cols="27" rows="5" />
                 <tr>
				<td valign="top">Prescription:</td>
				<td><table id="drugTable">
				
						<s:iterator value="selectedDrugs">

								<tr><td><select name="drugId">

										<option value="-1">--</option>

										<s:iterator value="retrievedDrugs">

											<option value="<s:property value="drugId"/>"
												<s:if test="drugId==key">selected = "selected"</s:if>>
												<s:property value="drugName" />
											</option>
										</s:iterator>

								</select></td>
								<td><input type="text" name="dosage"
									value="<s:property value='value'/>"></td>
								<td><a href="#" onClick="removeDrugFromTable(this.parentNode.parentNode)"> delete</a></td>
							</tr>
						</s:iterator>
						<tr>
							<td><a href="#" onClick="addRowToTable()"> Add New Drug</a></td>
						</tr>
						
					</table>
				</td>
                <s:token name="token"></s:token>
                     <s:submit value="Update"/>
                </tr>
			   </s:form>
			   <div id="actionResult"><s:property value="#session.actionResult"/></div>
			</center>	
		</div>
</body>
</html>